package com.beidasoft.web.util;

import com.beidasoft.web.exception.common.CommonException;
import com.beidasoft.web.model.system.User;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class SessionUtil {
    public static HttpSession getHttpSession() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return request.getSession();
    }

    public static User getHttpSessionUser() throws Exception {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        User user = null;
        Object userObject = request.getSession().getAttribute("user");
        if (userObject == null) {
            String exceptionInfo = "会话获取用户信息为空值";
            Log4jUtil.COMMON_LOG.error(exceptionInfo);
            throw new CommonException(exceptionInfo);
        }
        user = (User) userObject;
        return user;
    }
}
